package com.shenfa.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.City;
import com.shenfa.bean.PaperCityCrd;
import com.shenfa.bean.TreeNodeCheck;
import com.shenfa.dao.CityDao;
import com.shenfa.dao.EpibCityDao;

/**
 * 外包商城市关系业务逻辑类
 * 
 * @author yanglong
 * 
 * @date 2011-8-22
 *
 */
public class EpibCityService {
	
	@Autowired
	private EpibCityDao epibCityDao;
	
	@Autowired
	private CityDao cityDao;
	
	/**
	 * 根据对象条件查询外包商作业分配列表
	 */
	public List<PaperCityCrd> getList(PaperCityCrd t, int start, int limit) {
		return epibCityDao.getList(t, start, limit);
	}
	public int getCount(PaperCityCrd t) {
		return epibCityDao.getCount(t);
	}

	/**
	 * 添加外包商作业分配信息
	 * @param t	作业分配
	 * @return	1成功，0失败
	 */
	public int add(PaperCityCrd t) {
		String[] citynos = t.getCity_no().split("\\|");
		int flag = 0;
		for(int i = 0; i < citynos.length; i++){
			t.setCity_no(citynos[i]);
			flag = epibCityDao.add(t);
			if(flag == 0){
				return 0;
			}
		}
		return flag;
	}

	/**
	 * 删除外包商作业分配信息
	 * @param t	作业分配
	 * @return	1成功，0失败
	 */
	public int del(PaperCityCrd t) {
		return epibCityDao.del(t);
	}

	/**
	 * 修改外包商作业分配信息
	 * @param t	作业分配
	 * @return	1成功，0失败
	 */
	public int upd(PaperCityCrd t) {
		String[] citynos = t.getCity_no().split("\\|");
		int flag = 0;
		int type = epibCityDao.updNull(t);
		if(type == 1) {
			for(int i = 0; i < citynos.length; i++){
				t.setCity_no(citynos[i]);
				flag = epibCityDao.upd(t);
				if(flag == 0){
					return 0;
				}
			}
		}
		return 1;
	}

	/**
	 * 根据对象条件查询卡产品对应的城市发行树
	 * @param paperCityCrd	
	 * @return
	 */
	public List<TreeNodeCheck> queryTreeList(PaperCityCrd t) {
		//查询所有卡产品指定外包商可选发行城市列表
		List<City> cityList = epibCityDao.getAllList(t);
		//查询指定卡产品指定外包商对应城市编号
		List<String> citynolist= epibCityDao.getNoList(t); 
		
		//取出发行城市编号
		Map<String, String> map = new HashMap<String, String>();
		for(String s : citynolist){
			map.put(s, s);
		}
		//返回对应发布城市多选树
		List<TreeNodeCheck> cityCheckList = new ArrayList<TreeNodeCheck>();
		
		//构造省级节点
		List<City> shengList=epibCityDao.getSheng(cityList);
		for(City c:shengList){
			TreeNodeCheck tnc1 = new TreeNodeCheck();
			List<TreeNodeCheck> tmpList = new ArrayList<TreeNodeCheck>();
			tnc1.setId(c.getCityno());
			tnc1.setText(c.getCityname());
			tnc1.setLeaf(true);
			tnc1.setChildren(tmpList);
			cityCheckList.add(tnc1);
		}

		TreeNodeCheck tnc;
		String cityno = "";
		//购置返回树
		for(City city : cityList){
			tnc = new TreeNodeCheck();
			cityno = city.getCityno();
			tnc.setId(cityno);
			tnc.setText(city.getCityname());
			tnc.setLeaf(city.getFlag());
			//如果城市已选
			if(map.containsKey(cityno)){
				tnc.setChecked(true);
			}
			//如果为根节点
			if("0".equals(city.getParentno())){
				if("1".equals(city.getFlag())){
					List<TreeNodeCheck> tmpList = new ArrayList<TreeNodeCheck>();
					tnc.setChildren(tmpList);
				}
				cityCheckList.add(0,tnc);
			}
			else {
				for(int i=0;i<cityCheckList.size();i++){
					if(cityCheckList.get(i).getId().equals(city.getParentno())){
						cityCheckList.get(i).setChecked(true);
						cityCheckList.get(i).getChildren().add(tnc);
						break;
					}
				}
			}
		}
		return cityCheckList;
	}
	
	/**
	 * 根据对象条件查询卡产品对应的城市发行树
	 */
	public List<TreeNodeCheck> queryTreeList1(PaperCityCrd t) {
		//指定卡产品指定预印纸可选发行城市列表
		List<City> cityList = epibCityDao.getAllList1(t);
		//指定卡产品指定预印纸对应城市编号
		List<String> citynolist= epibCityDao.getNoList1(t); 
		//取出发行城市编号
		Map<String, String> map = new TreeMap<String, String>();
		for(String s : citynolist){
			map.put(s, s);
		}
		//返回对应发布城市多选树
		List<TreeNodeCheck> cityCheckList = new ArrayList<TreeNodeCheck>();
		//临时对象
		Map<String, TreeNodeCheck> tmpMap = new HashMap<String, TreeNodeCheck>();
		
		TreeNodeCheck tnc;
		String cityno = "";
		//购置返回树
		for(City city : cityList){
			tnc = new TreeNodeCheck();
			cityno = city.getCityno();
			tnc.setId(cityno);
			tnc.setText(city.getCityname());
			tnc.setLeaf(city.getFlag());
			//如果城市已选
			if(map.containsKey(cityno)){
				tnc.setChecked(true);
			}
			//如果为根节点
			if("0".equals(city.getParentno())){
				tmpMap.put(cityno, tnc);
			} else {
				//不存在跟节点,将根城市添加
				if(!map.containsKey(city.getParentno())){
					City c = new City();
					c.setCityno(city.getParentno());
					List<City> list = cityDao.queryList(c, 0, 10);
					if(list != null){
						c = list.get(0);
						TreeNodeCheck tnc1 = new TreeNodeCheck();
						tnc1.setId(c.getCityno());
						tnc1.setText(c.getCityname());
						tnc1.setLeaf(true);
						tnc1.setChecked(false);
						tmpMap.put(c.getCityno(), tnc1);
						List<TreeNodeCheck> tmpList = new ArrayList<TreeNodeCheck>();
						tmpList.add(tnc);
						tmpMap.get(c.getCityno()).setChildren(tmpList);
						map.put(c.getCityno(), c.getCityno());
						if(tnc.isChecked()){
							tmpMap.get(c.getCityno()).setChecked(true);
						}
					}
				} else {  //已存在根节点
					tmpMap.get(city.getParentno()).getChildren().add(tnc);
					if(tnc.isChecked()){
						tmpMap.get(city.getParentno()).setChecked(true);
					}
				}
			}
		}
		//获取要返回的树
		for(Entry<String, TreeNodeCheck> en : tmpMap.entrySet()){
			cityCheckList.add(en.getValue());
		}
		return cityCheckList;
	}
	
	
}
